package com.lovely602.langchain4j.param.controller.chat;


import dev.langchain4j.data.message.UserMessage;
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.model.chat.response.ChatResponse;
import dev.langchain4j.model.output.TokenUsage;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 *
 * @author lizhixing
 */
@Slf4j
@RestController
@RequestMapping(value = "/low")
public class LowApiController {

    @Resource(name = "chatModelQwen")
    private ChatModel chatModelQwen;

    @Resource(name = "chatModelDeepSeek")
    private ChatModel chatModelDeepSeek;


    /**
     * 调用底层模型聊天接口
     * @param msg
     * @return
     */
    @GetMapping(value = "/chat01")
    public String api01(@RequestParam(value = "msg", defaultValue = "你是谁") String msg) {
        String result = chatModelQwen.chat(msg);

        System.out.println("通过langchain4j调用模型返回结果：" + result);

        return result;
    }

    /**
     * Token用量计算的底层api演示验证聊天案例
     *
     */
    @GetMapping(value = "/chat02")
    public String api02(@RequestParam(value = "msg", defaultValue = "你是谁") String msg) {
        ChatResponse chatResponse = chatModelDeepSeek.chat(UserMessage.from(msg));

        String result = chatResponse.aiMessage().text();
        System.out.println("通过调用大模型返回结果：" + result);

        // Token 用量计算的底层api
        TokenUsage tokenUsage = chatResponse.tokenUsage();

        System.out.println("本次调用消耗的token：" + tokenUsage);

        result = result + "\t\n" + tokenUsage;

        return result;
    }

}
